//
//  ZTodoNotifyVC.m
//  WeClubs
//
//  Created by Zed Link on 8/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoNotifyVC.h"
#import "ZTodoNotifyCell.h"
#import "ZTodoFlowLayout.h"
#import "ZTodoNotifyDetailVC.h"

static NSString *NotifyCellReuseID = @"ZNotifyCellReuseID";

@interface ZTodoNotifyVC () <UICollectionViewDelegate, UICollectionViewDataSource>

/** 待办通知列表 */
@property (weak, nonatomic) UICollectionView *notifyView;

/** 待办通知数据源 */
@property (strong, nonatomic) NSArray <ZTodoNotifyModel *> *notifyModels;

@end

@implementation ZTodoNotifyVC

- (void)zLoadSubview
{
    [super zLoadSubview];
    
    // ========== 待办通知列表 ==========
    ZTodoFlowLayout *layout = [[ZTodoFlowLayout alloc] init];
    
    UICollectionView *notifyView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    notifyView.delegate = self;
    notifyView.dataSource = self;
    
    [notifyView setBackgroundColor:GENERAL_VIEW_BG_COLOR];
    [notifyView registerNib:[UINib nibWithNibName:@"ZTodoNotifyCell" bundle:nil] forCellWithReuseIdentifier:NotifyCellReuseID];
    notifyView.contentInset = UIEdgeInsetsMake(DYNAMIC_TOP_SPACE, 0, DYNAMIC_TOP_SPACE, 0);
    self.automaticallyAdjustsScrollViewInsets = NO;

    self.notifyView = notifyView;
    [self.view addSubview:notifyView];
    [self.notifyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSDictionary *dataPara = @{
                               @"user_id" : @([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"club_id" : @(self.clubModel.clubId),
                               @"dynamic_type" : @"notify",
                               @"page_no" : @(0),
                               @"size" : @(8),
                               };
    
    
    [HTTPRequest POST:kGet_dynamic_listUrl parameter:dataPara success:^(id resposeObject) {
        NSArray *notifys = resposeObject[@"data"][@"notify"];
        
        NSMutableArray *notifyModels = [NSMutableArray arrayWithCapacity:notifys.count];
        for (NSDictionary *dict in notifys) {
            ZTodoNotifyModel *model = [[ZTodoNotifyModel alloc] initWithDictionary:dict];
            [notifyModels addObject:model];
        }
        self.notifyModels = notifyModels;
    } failure:^(NSError *error) {
       
    }];
}

#pragma mark - UICollectionViewDataSource & UICollectionViewDelegate

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.notifyModels.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoNotifyModel *model = self.notifyModels[indexPath.row];
    
    ZTodoNotifyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NotifyCellReuseID forIndexPath:indexPath];
    cell.type = ZNotifyTypeTodo;
    cell.model = model;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZTodoNotifyModel *model = self.notifyModels[indexPath.row];
    
    ZTodoNotifyDetailVC *detailVC = [ZTodoNotifyDetailVC zNew];
    detailVC.notifyModel = model;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - Getter & Setter

- (void)setNotifyModels:(NSArray<ZTodoNotifyModel *> *)notifyModels
{
    _notifyModels = notifyModels;
    
    [self.notifyView reloadData];
}

@end
